package com.woniuxy.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.pojo.Scores;
import com.woniuxy.service.ScoresService;

@Controller
@RequestMapping("/scores")
public class ScoresController {

	@Autowired
	private ScoresService scoresService;
	
	@RequestMapping("/list")
	public String getAllScores(@RequestParam(required = false, defaultValue = "1") Integer startPage,
            @RequestParam(required = false, defaultValue = "5") Integer PageSize,Model model) {
		
		PageHelper.startPage(startPage, PageSize);
		
		List<Scores> l = scoresService.getAllScores();
		PageInfo<Scores> pageData = new PageInfo<Scores>(l);
		model.addAttribute("scores",pageData);
		return "forward:/train/scores.jsp";
	}
	
	@RequestMapping("/addScores")
	public String addScores() {
		
		scoresService.addScores();
		return "forward:/scores/list";
	}
	
	@RequestMapping("/updscoresUI")
	public String updscoresUI(HttpServletRequest req,Model model) {
		
		Integer scoresid = Integer.parseInt(req.getParameter("scores_id"));
		System.out.println("-------------------"+scoresid);
		Scores scores = scoresService.getScoresById(scoresid);
		model.addAttribute("scores",scores);
		System.out.println("------------"+scores);
		return "forward:/train/scores-upd.jsp";
	}
	
	@RequestMapping("/updScores")
	public String updScores(Scores scores) {
		
		scoresService.updScores(scores);
		
		return "";
	}
}
